package com.tiger.angrybirds;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.youmi.android.AdListener;
import net.youmi.android.AdView;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;

public class CommonUtil {
	static final String MP3 = ".mp3";

	/**
	 * 置入有米广告
	 * 
	 * @author:tiger.chenlin (2011-5-11)
	 * @file:CommonUtil.java
	 * @return:void
	 * @param act
	 * @param listener
	 */
	public static void setAdView(Activity act, AdListener listener) {
		// 初始化广告视图
		AdView adView = new AdView(act, Color.GRAY, Color.WHITE, 50);

		// 设置监听者(可以不设置，对获取广告没有影响)
		adView.setAdListener(listener);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);

		// 设置广告出现的位置(悬浮于底部)
		params.bottomMargin = 0;
		params.gravity = Gravity.BOTTOM;
		adView.setBackgroundColor(R.color.clr_blue);
		// 将广告视图加入Activity中
		act.addContentView(adView, params);
	}

	/**
	 * 判断文件是否存在，不存在则创建.
	 * 
	 * @param path
	 * @return
	 */
	private static boolean isFile(String path) {
		boolean b = false;
		File f = new File(path);
		if (f.exists()) {
			b = true;
		} else {
			if (f.mkdirs()) {
				b = true;
			} else {
				b = false;
			}
		}
		return b;
	}

	/**
	 * 将铃声copy到目标目录下 tiger 2011-7-2 上午11:43:42
	 * 
	 * @param filename
	 *            :filename.mp3
	 * @param origialPath
	 * @param aimPath
	 * @return
	 */
	public static File copyRingToAimdir(Activity context, String filename,
			int rawId, String aimPath) {
		try {
			filename = filename + MP3;
			InputStream ins = context.getResources().openRawResource(rawId);
			Log.i("zm", aimPath + "/" + filename);
			if (isFile(aimPath)) {
				File file = new File(aimPath + "/" + filename);
				OutputStream os = new FileOutputStream(file);
				int bytesRead = 0;
				byte[] buffer = new byte[8192];
				while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
					os.write(buffer, 0, bytesRead);
				}
				os.close();
				ins.close();
				return file;
			}
			return null;
		} catch (Exception exp) {
			exp.printStackTrace();
			return null;
		}
	}

	/**
	 * 设置电话铃声 tiger 2011-6-29 下午04:34:35
	 */
	public static boolean setCallRing(Activity context, String filename,
			int rawId) {
		try {

			String aimPath = Environment.getExternalStorageDirectory()
					+ "/ringtones";
			File file = copyRingToAimdir(context, filename, rawId, aimPath);
			// 插入铃声
			ContentValues values = new ContentValues();
			values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
			values.put(MediaStore.MediaColumns.TITLE, file.getName());
			values.put(MediaStore.MediaColumns.SIZE, file.length());
			values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
			// values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
			// values.put(MediaStore.Audio.Media.DURATION, 230);
			values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
			values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
			values.put(MediaStore.Audio.Media.IS_ALARM, false);
			values.put(MediaStore.Audio.Media.IS_MUSIC, false);

			Uri uri = MediaStore.Audio.Media.getContentUriForPath(file
					.getAbsolutePath());
			Uri newUri = context.getContentResolver().insert(uri, values);
			// 设置铃声
			RingtoneManager.setActualDefaultRingtoneUri(context,
					RingtoneManager.TYPE_RINGTONE, newUri);
			/**
			 * // 打开系统铃声设置 Intent intent = new Intent(
			 * RingtoneManager.ACTION_RINGTONE_PICKER); // 设置类型为来电
			 * intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
			 * RingtoneManager.TYPE_RINGTONE); // 设置显示的标题
			 * intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置来电铃声");
			 * context.startActivityForResult(intent, 0);
			 */
			return true;
		} catch (Exception exp) {
			exp.printStackTrace();
			return false;
		}
	}

	/**
	 * 设置短信铃声 tiger 2011-6-29 下午04:34:26
	 */
	public static boolean setSmsRing(Activity context, String filename, int rawId) {
		try {
			String aimPath = Environment.getExternalStorageDirectory()
					+ "/notifications";
			File file = copyRingToAimdir(context, filename, rawId, aimPath);
			// 插入铃声
			ContentValues values = new ContentValues();
			values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
			values.put(MediaStore.MediaColumns.TITLE, file.getName());
			values.put(MediaStore.MediaColumns.SIZE, file.length());
			values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
			// values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
			// values.put(MediaStore.Audio.Media.DURATION, 230);
			values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
			values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
			values.put(MediaStore.Audio.Media.IS_ALARM, false);
			values.put(MediaStore.Audio.Media.IS_MUSIC, false);
			
			Uri uri = MediaStore.Audio.Media.getContentUriForPath(file
					.getAbsolutePath());
			Uri newUri = context.getContentResolver().insert(uri, values);
			// 设置铃声
			RingtoneManager.setActualDefaultRingtoneUri(context,
					RingtoneManager.TYPE_NOTIFICATION, newUri);
			/**
			 * Intent intent = new
			 * Intent(android.media.RingtoneManager.ACTION_RINGTONE_PICKER);
			 * intent
			 * .putExtra(android.media.RingtoneManager.EXTRA_RINGTONE_TYPE,
			 * android.media.RingtoneManager.TYPE_NOTIFICATION);
			 * intent.putExtra(
			 * android.media.RingtoneManager.EXTRA_RINGTONE_TITLE,"设置通知铃声");
			 * startActivityForResult(intent, Notification);
			 */
			return true;
		} catch (Exception exp) {
			exp.printStackTrace();
			return false;
		}
	}

	/**
	 * 设置闹钟铃声 tiger 2011-6-29 下午04:34:11
	 */
	public static boolean setTimeRing(Activity context, String filename, int rawId) {
		try {
			String aimPath = Environment.getExternalStorageDirectory()
					+ "/alarms";
			File file = copyRingToAimdir(context, filename, rawId, aimPath);
			// 插入铃声
			ContentValues values = new ContentValues();
			values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
			values.put(MediaStore.MediaColumns.TITLE, file.getName());
			values.put(MediaStore.MediaColumns.SIZE, file.length());
			values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
			// values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
			// values.put(MediaStore.Audio.Media.DURATION, 230);
			values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
			values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
			values.put(MediaStore.Audio.Media.IS_ALARM, true);
			values.put(MediaStore.Audio.Media.IS_MUSIC, false);

			Uri uri = MediaStore.Audio.Media.getContentUriForPath(file
					.getAbsolutePath());
			Uri newUri = context.getContentResolver().insert(uri, values);
			// 设置铃声
			RingtoneManager.setActualDefaultRingtoneUri(context,
					RingtoneManager.TYPE_ALARM, newUri);
			/**
			 * Intent intent = new
			 * Intent(android.media.RingtoneManager.ACTION_RINGTONE_PICKER);
			 * intent
			 * .putExtra(android.media.RingtoneManager.EXTRA_RINGTONE_TYPE,
			 * android.media.RingtoneManager.TYPE_ALARM);
			 * intent.putExtra(android.
			 * media.RingtoneManager.EXTRA_RINGTONE_TITLE,"设置闹钟铃声");
			 * startActivityForResult(intent, Alarm);
			 */
			return true;
		} catch (Exception exp) {
			exp.printStackTrace();
			return false;
		}
	}

	public static int[] rawIds = { R.raw.s0, R.raw.s1, R.raw.s2, R.raw.s3,
			R.raw.s4, R.raw.s5, R.raw.s6, R.raw.s7, R.raw.s8, R.raw.s9,
			R.raw.s10, R.raw.s11, R.raw.s12, R.raw.s13, R.raw.s14, R.raw.s15,
			R.raw.s16, R.raw.s17, R.raw.s18, R.raw.s19, R.raw.s20, R.raw.s21,
			R.raw.s22, R.raw.s23, R.raw.s24, R.raw.s25, R.raw.s26, R.raw.s27,
			R.raw.s28, R.raw.s29, R.raw.s30, R.raw.s31, R.raw.s32, R.raw.s33,
			R.raw.s34, R.raw.s35, R.raw.s36, R.raw.s37, R.raw.s38, R.raw.s39,
			R.raw.s40, R.raw.s41, R.raw.s42, R.raw.s43, R.raw.s44, R.raw.s45,
			R.raw.s46, R.raw.s47, R.raw.s48, R.raw.s49, R.raw.s50, R.raw.s51,
			R.raw.s52, R.raw.s53, R.raw.s54, R.raw.s55, R.raw.s56, R.raw.s57,
			R.raw.s58, R.raw.s59, R.raw.s60, R.raw.s61, R.raw.s62, R.raw.s63,
			R.raw.s64, R.raw.s65, R.raw.s66, R.raw.s67, R.raw.s68, R.raw.s69,
			R.raw.s70, R.raw.s71, R.raw.s72, R.raw.s73, R.raw.s74, R.raw.s75,
			R.raw.s76, R.raw.s77, R.raw.s78, R.raw.s79, R.raw.s80, R.raw.s81,
			R.raw.s82, R.raw.s83, };

	/**
	 * 得到music目录 tiger 2011-6-29 下午03:39:31
	 * 
	 * @param context
	 * @return
	 */
	public static List<Music> getMusicmenu(Context context) {
		List<Music> infos = new ArrayList<Music>();
		Music info = null;
		try {
			String[] arr = context.getResources().getStringArray(
					R.array.music_menu);
			for (int i = 0; i < arr.length; i++) {
				info = new Music(i, arr[i], false);
				infos.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return infos;
	}
}
